<?php

use yii\helpers\Html;
use yii\grid\GridView;
use yii\data\ActiveDataProvider;
use yii\widgets\Pjax;
use common\widgets\DetailView;

/* @var $this yii\web\View */
/* @var $model backend\models\AuthItem */

$this->title = '查看角色';
$this->params['breadcrumbs'][] = ['label' => 'Auth Items', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
\yii\web\YiiAsset::register($this);
?>
<div class="auth-item-view">
    <?php Pjax::begin(); ?>

    <div class="panel">
		<div class="panel-heading">
            <?= Html::encode($this->title) ?>          
            <div class="pull-right">                
                <?= Html::a('修改', ['update', 'id' => $model->name], ['class' => 'btn btn-sm btn-cyan']) ?>
                <?= Html::a('删除', ['delete', 'id' => $model->name], [
                    'class' => 'btn btn-sm btn-danger',
                    'data' => [
                        'confirm' => 'Are you sure you want to delete this item?',
                        'method' => 'post',
                    ],
                ]) ?>
            </div>
		</div>
        <div class="panel-body">
            <?= DetailView::widget([
                'model' => $model,
                'fieldRows' => [
                    [
                        '角色' => $model->name,
                        '规则' => $model->rule_name,
                    ],[
                        '说明' => $model->description,
                        '附加数据' => $model->data,
                    ],[
                        '创建时间:datetime' => $model->created_at,
                        '修改时间:datetime' => $model->updated_at,
                    ],
                ],
            ]) ?>
        </div>
    </div>

    <div class="panel">
		<div class="panel-heading">
            包含的角色与权限       
            <div class="pull-right">   
                <?= Html::a('添加角色', ['auth', 'id' => $model->name], ['class' => 'btn btn-auth-role btn-sm btn-cyan']) ?>
                <?= Html::a('添加权限', ['auth', 'id' => $model->name], ['class' => 'btn btn-auth-perm btn-sm btn-cyan']) ?>
                <?= Html::beginForm(['auth', 'id' => $model->name], 'post', ['class'=>'auth-form']) ?>
                <?= Html::endForm() ?>
            </div>
		</div>
        <div class="panel-body">
            <?= GridView::widget([
                'layout' => "{items}\n{pager}",
                'tableOptions' => ['class' => 'table table-hover table-bordered'],
                'dataProvider' => new ActiveDataProvider(['query' => $model->getChildren()]),
                //'filterModel' => $searchModel,
                'columns' => [
                    ['class' => 'yii\grid\SerialColumn'],

                    [
                        'attribute' => 'name',
                        'label' => '名称',
                    ],
                    
                    [
                        'attribute' => 'type',
                        'label' => '类型',
                        'value' => function($item){ return $item->type===1?'角色':'权限'; },
                    ],

                    [
                        'class' => 'yii\grid\ActionColumn', 
                        'template' => '{delete}',
                        'contentOptions' => ['class'=>'text-right'],
                        'buttons' => [
                            'delete' => function ($url, $item, $key)use($model) {
                                return Html::a('撤销', 
                                    ['un-auth', 'id'=>$model->name, 'item_name'=>$item->name],
                                    [
                                        'class' => 'btn btn-xs btn-danger',
                                        'data' => [
                                            'confirm' => '您确定要撤销此项授权吗？',
                                            'method' => 'post',
                                        ]
                                    ]
                                );
                            },
                        ],
                    ],
                ],
            ]); ?>
        </div>
    </div>
    
    <div class="panel">
		<div class="panel-heading">
            此角色的用户       
            <div class="pull-right">                
                <?= Html::a('添加', ['assign', 'name' => $model->name], ['class' => 'btn btn-sm btn-cyan']) ?>
            </div>
		</div>
        <div class="panel-body">
            <?= GridView::widget([
                'layout' => "{items}\n{pager}",
                'tableOptions' => ['class' => 'table table-hover table-bordered'],
                'dataProvider' => new ActiveDataProvider(['query' => $model->getUsers()]),
                //'filterModel' => $searchModel,
                'columns' => [
                    ['class' => 'yii\grid\SerialColumn'],

                    'username',

                    [
                        'class' => 'yii\grid\ActionColumn', 
                        'template' => '{delete}',
                        'contentOptions' => ['class'=>'text-right'],
                        'buttons' => [
                            'delete' => function ($url, $user, $key)use($model) {
                                return Html::a('删除', 
                                    ['un-assign', 'name'=>$model->name, 'user_id'=>$user->id],
                                    [
                                        'class' => 'btn btn-xs btn-danger',
                                        'data' => [
                                            'confirm' => 'Are you sure you want to delete this item?',
                                            'method' => 'post',
                                        ]
                                    ]
                                );
                            },
                        ],
                    ],
                ],
            ]); ?>
        </div>
    </div>
    <?php Pjax::end(); ?>
    
    <?php $this->render('//common/dialog'); ?>

    <script>
    <?php $this->beginBlock('auth-item-form-js') ?>
        // 为用户添加授权
        function addAuthItems(items){
            var $form = $('.auth-form');            
            items.forEach(item=>{
                $form.append('<input type="hidden" name="items[]" value="'+item+'"/>');
            });
            $form.submit();  // 提交表单
        }

        // 选择角色，添加授权
        $('.btn-auth-role').on('click', function(){
            commonDialog('/admin/role/list', {title: '选择角色'}, addAuthItems);
            return false;
        });

        // 选择权限，添加授权
        $('.btn-auth-perm').on('click', function(){
            commonDialog('/admin/permission/list', {title: '选择权限'}, addAuthItems);
            return false;
        });
    <?php $this->endBlock(); ?>
    <?php $this->registerJs($this->blocks['auth-item-form-js']);?>
    </script>
</div>
